package com.mallcloud.mall.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallcloud.mall.app.api.entity.vo.ProOrderCouponRefundDone;
import com.mallcloud.mall.app.mapper.AppOrderCouponRefundMapper;
import com.mallcloud.mall.app.mapper.AppProcedureMapper;
import com.mallcloud.mall.app.service.AppOrderCouponRefundService;
import com.mallcloud.mall.common.core.constant.enums.mall.OrderEnum;
import com.mallcloud.mall.common.core.exception.FailException;
import com.mallcloud.mall.mall.api.entity.MallOrderCouponRefundModel;
import com.mallcloud.mall.mall.api.entity.MallPointsCouponOfRefundModel;
import com.mallcloud.mall.mall.service.MallPointsCouponOfRefundService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 券退款单
 */
@Slf4j
@Service
@AllArgsConstructor
public class AppOrderCouponRefundServiceImpl extends ServiceImpl<AppOrderCouponRefundMapper, MallOrderCouponRefundModel> implements AppOrderCouponRefundService {

	private final AppOrderCouponRefundMapper appOrderCouponRefundMapper;
	private final AppProcedureMapper appProcedureMapper;
	private final MallPointsCouponOfRefundService mallPointsCouponOfRefundService;

	@Override
	public void updateRefundInfo(ProOrderCouponRefundDone dto) {
		appProcedureMapper.updateRefundInfo(dto);
		if (!dto.isSuccess()) {
			log.error("券退款信息修改失败");
			throw new FailException(dto.getMessage());
		}
	}

	@Override
	public void updateTryFail(String orderNo, Integer status, String errMsg) {
		appOrderCouponRefundMapper.updateTryFail(orderNo, status, errMsg);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void handleNotifyByWxRefund(MallOrderCouponRefundModel orderCouponRefund) {
		String orderNo = orderCouponRefund.getOrderNo();

		log.info("[{}]订单数据：{}", orderNo, JSON.toJSONString(orderCouponRefund));
		if (OrderEnum.StatusRefundEnum.SUCCESS.getStatus().equals(orderCouponRefund.getStatus())) {
			log.error("[{}]订单重复回调拦截", orderNo);
			return;
		}
		//调修改过程
		List<MallPointsCouponOfRefundModel> refundCoupons = mallPointsCouponOfRefundService.list(new LambdaQueryWrapper<MallPointsCouponOfRefundModel>()
				.eq(MallPointsCouponOfRefundModel::getOrderNo, orderNo)
		);
		refundCoupons.forEach(x -> this.updateRefundInfo(new ProOrderCouponRefundDone(x.getId(), orderNo)));
		//更新订单状态
		this.update(new LambdaUpdateWrapper<MallOrderCouponRefundModel>()
				.eq(MallOrderCouponRefundModel::getId, orderCouponRefund.getId())
				.set(MallOrderCouponRefundModel::getStatus, OrderEnum.StatusRefundEnum.SUCCESS.getStatus())
		);
	}

	@Override
	public void refundSuccess(String orderNo, Date successTime) {
		//调修改过程
		List<MallPointsCouponOfRefundModel> refundCoupons = mallPointsCouponOfRefundService.list(new LambdaQueryWrapper<MallPointsCouponOfRefundModel>()
				.eq(MallPointsCouponOfRefundModel::getOrderNo, orderNo)
		);
		refundCoupons.forEach(x -> this.updateRefundInfo(new ProOrderCouponRefundDone(x.getId(), orderNo)));
		//更新订单状态
		this.update(new LambdaUpdateWrapper<MallOrderCouponRefundModel>()
				.eq(MallOrderCouponRefundModel::getOrderNo, orderNo)
				.set(MallOrderCouponRefundModel::getStatus, OrderEnum.StatusRefundEnum.SUCCESS.getStatus())
				.set(MallOrderCouponRefundModel::getRefundTime, successTime)
				.set(MallOrderCouponRefundModel::getHasRefunded, true)
		);
	}
}
